package com.flyduck.modules.system.web;

import com.flyduck.common.annotation.Log;
import com.flyduck.modules.system.domain.SysDept;
import com.flyduck.modules.system.service.SysDeptService;
import com.flyduck.common.vo.AjaxResult;
import com.flyduck.common.vo.QueryVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Set;

/**
 * @program: flyduck-admin
 * @description: 部门管理
 * @author: flyduck
 * @create: 2020-05-06 15:12
 **/
@Controller
@RequestMapping("/dept")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @GetMapping

    public String index(){
        return "system/dept";
    }

    /**
     * 跳转到首页
     * @param queryVo
     * @return
     */
    @Log(value = "跳转到部门管理页面")
    @RequiresPermissions("dept:list")
    @GetMapping("/list")
    @ResponseBody
    public Set<SysDept> findList(QueryVo queryVo){
        return sysDeptService.findTree(queryVo);
    }

    /**
     * 根据id查询部门
     * @param id
     * @return
     */
    @Log(value = "根据id查询部门")
    @GetMapping("/getDeptById")
    @ResponseBody
    public SysDept getDeptById(Long id){
        return sysDeptService.getDeptById(id);
    }

    /**
     * 保存部门
     * @param sysDept
     * @return
     */
    @Log(value = "保存部门")
    @RequiresPermissions("dept:add")
    @GetMapping("/save")
    @ResponseBody
    public AjaxResult save(SysDept sysDept){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysDeptService.save(sysDept);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("保存失败");
        }

        return ajaxResult;
    }

    /**
     * 更新部门
     * @param sysDept
     * @return
     */
    @Log(value = "更新部门")
    @RequiresPermissions("dept:edit")
    @GetMapping("/update")
    @ResponseBody
    public AjaxResult update(SysDept sysDept){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysDeptService.update(sysDept);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("更新失败");
        }

        return ajaxResult;
    }

    /**
     * 根据部门id删除当前及以下的所有部门
     * @param id
     * @return
     */
    @Log(value = "根据部门id删除当前及以下的所有部门")
    @RequiresPermissions("dept:del")
    @PostMapping("delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysDeptService.deleteById(id);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("删除失败");
        }

        return ajaxResult;
    }
}
